Ext.ns('common');

//Ajax loading cofig
Ext.Ajax.on('beforerequest', function (conn, options) {
	
	// connection timeout
	conn.timeout = Number(common.timeout);
	
	// get connection params
	var opts = options.params || {};
	
	// add params
	var params = {isAjax: 1};
	
	// set params
	Ext.apply(opts, params);
	
	if(common.auto_show_loading) {
		common.loading(true);
	}
	
}, this);

Ext.Ajax.on('requestcomplete', function (conn, response, options) {
	common.loading(false);
	
}, this);

Ext.Ajax.on('requestexception ', function (conn, response, options, e) {
	
	common.loading(false);
}, this);

common.loading = function (status) {
    if (status) {
        //Ext.get(document.body).mask(Ext.LoadMask.prototype.msg);
    	$('#mask-loading-image').html(Ext.LoadMask.prototype.msg);
    	//$('#loading').fadeIn("fast");
    	$('#loading').show();
    }
    else {
        //Ext.get(document.body).unmask();
    	//$('#loading').fadeOut("slow");
    	$('#loading').hide();
    }
}
common.paging = function(store) {
	var paging = new Ext.PagingToolbar( {
		pageSize: Number(common.rec_per_page),
		store : store,
		displayInfo : true
	});
	
	return paging;
}
common.pagingPageSize = function(store) {
	
	var paging = new Ext.PagingToolbar( {
		pageSize: Number(common.rec_per_page),
		store : store,
		displayInfo : true,
		
		plugins: [
	      new Ext.ux.plugins.PageComboResizer({pageSizes: [ 10, 25, 50, 100],
		  prefixText: 'Page size',
		  postfixText: ''})
	    ]
	});
	
	return paging;
}

/** Function: common.setCheckAll: set checkbox checked
 	
 	Params:
 		chkName: string - checkbox name
 		
 	Return: void	
**/
common.setCheckAll = function(chkName) {
    var list = document.getElementsByName(chkName);

    for (i = 0; i < list.length; i++) {
        list[i].checked = true;
    }
}
common.getValueAll = function(txtName) {
	var list = document.getElementsByName(txtName);
	var value= "";
	for( var i=0; i <list.length; i++){
		if(list[i].value){
			value += list[i].value + ",";
		}else{
			value += -1 + ",";
		}
	}
	if(ids.length > 1) {
    	value = value.substr(0, ids.length - 1);
    }
	return value;
}

common.setUnCheckAll = function(chkName) {
    var list = document.getElementsByName(chkName);
    for (i = 0; i < list.length; i++) {
        list[i].checked = false;
    }
}
common.getListboxAllItems = function(listId) {
	var ids = "";
	
	var listbox = document.getElementById(listId);
    
	for (var i = 0; i < listbox.options.length; i++) {
        ids += listbox.options[i].value + ",";
    }
	
    if(ids.length > 1) {
    	ids = ids.substr(0, ids.length - 1);
    }
    
    return ids;
}
common.getListboxSelectedItems = function(listId) {
	var ids = "";
	
	var listbox = document.getElementById(listId);
	
    for (var i = 0; i < listbox.options.length; i++) {
    	if(listbox.options[i].selected == true) {
    		ids += listbox.options[i].value + ",";
    	}
    }
	
    if(ids.length > 1) {
    	ids = ids.substr(0, ids.length - 1);
    }
    
    return ids;
}

common.getListboxSelect = function(listId) {
    var listbox = document.getElementById(listId);
    var ret = new Array();
    for (var i = 0; i < listbox.options.length; i++) {
        if (listbox.options[i].selected) {
            ret.push(listbox.options[i].value);
        }
    }
    return ret;
}
common.displayErrors = function(errors_id, errors)
{
	var error_list = "";
		
	try{
		$(".input-error").removeClass("input-error");
			
		for(var i = 0; i < errors.length; i++)
		{
			if(errors[i].key.trim() != "" )
				$("#" + errors[i].key).addClass("input-error");
			
			if(errors[i].value.trim() != "")
			{
				error_list += "<li>" + errors[i].value + "</li>";
			}
		}
		
		$("#" + errors_id).html(error_list);
		$("#" + errors_id).show();
	}
	catch(ex)
	{
		alert("ERROR: " + ex);
	}
}
common.clearErrors = function(errors_id) {
	$("#" + errors_id).empty();
	$(".input-error").removeClass("input-error");
	$("#" + errors_id).hide();
}
common.create_window=function (window_id, title, url, width, height) {
	var timestamp = Number(new Date());		
	
	if (url.indexOf('?') > 0) {
		var end_char = url.substr(url.length - 1, 1);
		if (end_char == '?'){
			url = url + 'temp=' + timestamp;
		}
		else if(end_char == '&'){
			url = url + 'temp=' + timestamp;
		}			
		else {
			url = url + '&temp=' + timestamp;
		}
	}
	else {
		url = url + '?temp=' + timestamp;
	}	
	
	var window;
	var _width = 800;
	var _height = 600;
	
	if(width != '' && width >= 0) {
		_width = width;
	}
	
	if(height != '' && height >= 0) {
		_height = height;
	}
	
	var x_p = screen.width;
	var y_p = screen.height;
	var px = (x_p - _width)/2;
	var py = (y_p - _height - 200)/2;
	
	if(!window)
	{	
		window = new Ext.Window({
	        id: window_id,
	        title: title,
	        width: _width,
	        height: _height,
	        layout: 'fit',
	        maximizable: false,
	        modal: true,
	        constrainHeader: true,
	        constrain: true,
	        autoScroll: true,
	        closeAction:'destroy',
	        closable:true,
	        resizable: false,
			x:px,
			y:py,
	        autoLoad : {
	            url : url,
	            scripts: true
	       
        	}
		});
	}
	
	return window;
}
common.checkExtension = function(file_name, exts) {
    file_name = file_name.substring(file_name.lastIndexOf('.'), file_name.length).toLowerCase();

    if(exts.indexOf(file_name) < 0) {
    	return false; 
    }
    
    return true;
}
common.isImageFile = function(file_name)
{
	var exts = new Array('.png', '.gif', '.jpg', '.jpeg', '.bmp');
	return common.checkExtension(file_name, exts);
}
common.addOption = function(selectbox, text, value ) {
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}
common.removeOptions = function(selectbox, i) {
	selectbox.remove(i);
}
	
	this.addErrors = function(ctrlId, msg) {
		var error = new Object();
		
		error.key = ctrlId;
		error.value = msg;
		
		errors.push(error);
	}

	this.writeErrors = function(errorsId) {
		var html = "";
		
		$(".input-error").removeClass("input-error");
			
		for(var i = 0; i < errors.length; i++) {
			if(errors[i].key.trim() != "" )
				$("#" + errors[i].key).addClass("input-error");
			
			if(errors[i].value.trim() != "") {
				html += "<li>" + errors[i].value + "</li>";
			}
		}
		
		$("#" + errorsId).html(html);
		$("#" + errorsId).show();
	}
common.create_window_html = function(window_id, title, html, width, height) {
	
	var window;
	var _width = 800;
	var _height = 600;
	
	if(width != '' && width >= 0) {
		_width = width;
	}
	
	if(height != '' && height >= 0) {
		_height = height;
	}
	
	
	if(!window)
	{	
		window = new Ext.Window({
	        id: window_id,
	        title: title,
	        width: _width,
	        height: _height,
	        layout: 'fit',
	        html: html,
	        maximizable: false,
	        modal: true,
	        constrainHeader: true,
	        constrain: true,
	        closeAction:'destroy',
	        autoScroll: true,
	        resizable: false
		});
	}
	
	return window;
}
common.getChecked = function(childName) {
	var selected = $("input[name=" + childName + "]:checked");

    var ids = '';
    for (var i = 0; i < selected.length; i++) {
        ids += selected[i].value + ",";
    }
    return ids.substring(0, ids.length - 1);
}
common.setCheckItem = function(ids,list_chk){
	var list = ids.split(",");
	for(var i =0; i< list.length; i++){
		for(var j =0; j < list_chk.length;j++){
			if(list_chk[j].value == list[i]){
				list_chk[j].checked = true;
			}
		}
	}
}

common.setCheckAll = function(chkName) {
    var list = document.getElementsByName(chkName);

    for (i = 0; i < list.length; i++) {
        list[i].checked = true;
    }
}